<?php
/* 
 * Copyright (c) 2018-2021   All rights reserved.
 * 
 * 创建时间：2021-05-16 09:52
 *
 * 项目：upload  -  $  - export_datatable.php
 *
 * 作者：liwei 
 */

use lev\base\Modelv;

?>

<div class="page page-formb">
    <div class="navbar page-admin-navbar adminbar navbar-bgcolor-red">
        <div class="navbar-inner">
            <div class="left">
                <a class="link tooltip-init" href="javascript:window.history.back();" data-tooltip="后退">
                    <svg class="icon" aria-hidden="true"><use xlink:href="#fa-back"></use></svg>
                </a>
                <a class="link tooltip-init" href="javascript:window.location.reload();" data-tooltip="刷新">
                    <svg class="icon" aria-hidden="true"><use xlink:href="#fa-refresh"></use></svg>
                </a>
            </div>
            <div class="title">
                <?php echo Lev::$app['title']?>
                <tips class="date" style="color:red !important;"><?php echo $tips?></tips>
            </div>
            <div class="right">
                <label class="button button-fill color-black wd100 scale9" onclick="checkedToggle('.ckAllTabs', 'input[name=\'tabs[]\']')"><input type="checkbox" class="ckAllTabs">全部选中</label>
                <a class="button-fill button color-red wd80 scale9" onclick="doSelectExport()">导出选中</a>
            </div>
        </div>
    </div>

    <div class="page-content">
        <div class="data-xtable">
            <table>
                <tr><td></td>
                    <td style="text-align: center;">
                        <tips>点击表名可直接导出</tips>
                    </td>
                    <td>
                    </td>
                </tr>
                <?php if ($myTables):?>
                    <?php foreach ($myTables as $v) : if ($v == '{{%}}') continue;$_yes=1;?>
                        <tr><td></td>
                            <td class="numeric-cell wd100">
                                <a class="link button-fill button color-blue" onclick="doExportTable('<?=$v?>')">导出表 <?=$v?> 数据</a>
                            </td>
                            <td><label><input type="checkbox" name="tabs[]" value="<?=$v?>"></label></td>
                        </tr>
                    <?php endforeach?>
                <?php endif;?>
                <?php if ($noTables):?>
                    <tr><td></td><td style="text-align: center"><tips>疑似模块表(后期程序安装)</tips></td><td></td></tr>
                    <?php foreach ($noTables as $v) : if ($v == '{{%}}') continue;$_yes=1;?>
                        <tr>
                            <td></td>
                            <td class="numeric-cell wd100"><a class="button-fill button color-gray" onclick="doExportTable('<?=$v?>')">导出表 <?=$v?> 数据</a></td>
                            <td><label><input type="checkbox" name="tabs[]" value="<?=$v?>"></label></td>
                        </tr>
                    <?php endforeach?>
                <?php endif;?>
                <?php if ($parentTables):?>
                    <tr><td></td><td style="text-align: center"><tips>父模块表</tips></td><td></td></tr>
                    <?php foreach ($parentTables as $v) : if ($v == '{{%}}') continue;$_yes=1;$v = Modelv::tableName($v);?>
                        <tr>
                            <td></td>
                            <td class="numeric-cell wd100"><a class="button-fill button color-gray" onclick="doExportTable('<?=$v?>')">导出表 <?=$v?> 数据</a></td>
                            <td><label><input type="checkbox" name="tabs[]" value="<?=$v?>"></label></td>
                        </tr>
                    <?php endforeach?>
                <?php endif;?>
                <?php if ($allTables):?>
                    <tr><td></td><td style="text-align: center"><tips>库内所有表</tips></td><td></td></tr>
                    <?php foreach ($allTables as $v) : if ($v == '{{%}}') continue;$_yes=1;$v = Modelv::tableName($v);?>
                        <tr>
                            <td></td>
                            <td class="numeric-cell wd100"><a class="button-fill button color-gray" onclick="doExportTable('<?=$v?>')">导出表 <?=$v?> 数据</a></td>
                            <td><label><input type="checkbox" name="tabs[]" value="<?=$v?>"></label></td>
                        </tr>
                    <?php endforeach?>
                <?php endif;?>
                <tr><td colspan="3" style="text-align: center"><?=isset($_yes)?'':'<tips>该模块没有创建表</tips>'?></td></tr>
            </table>
        </div>
    </div>
</div>

<script>
    function doSelectExport() {
        var tabNames = '';
        jQuery('input[name="tabs[]"]:checked').each(function () {
            tabNames += this.value + ',';
        })
        if (tabNames === "") {
            return levtoast('你至少选中一项');
        }
        doExportTable(tabNames);
    }
    function doExportTable(tablename) {
        levConfirm('此操作将覆盖原有数据，你确定要执行吗？', '', function () {
            actionExport(tablename);
        });
    }
    function actionExport(tablename, _page, _tabKey) {
        var dpage = _page ? _page : 1;
        showIconLoader();
        jQuery.ajax({
            url:'',
            data: {tablename:tablename, page:dpage, tabKey:_tabKey, dosubmit:1, inajax:1, _csrf:_csrf, _:Math.random()},
            dataType: 'json',
            type: 'post',
            success: function (data) {
                hideIconLoader();
                if (data.tablename) {
                    levtoast('<br>第'+ dpage +'页导出完毕，1秒后导出下一页！'+ data.message);
                    window.setTimeout(function () {
                        actionExport(data.tablename, data.page, data.tabKey);
                    }, 1000)
                }else {
                    levtoast('导出完成！'+data.message, 60000);
                }
            },
            error: function (data) {
                hideIconLoader();
                errortips(data);
            }
        });
    }

</script>

